﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XmlTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strFilePath = "D:\\xmltest\\test.xml";
            XmlTextReader objRd = new XmlTextReader(strFilePath);
            listBox1.Items.Clear();

            try
            {
                //List<string> templist = XmlReadAndWrite.getXmlElement(objRd);
                //listBox1.Items.Add(string.Join(",", templist.ToArray()));
                while (objRd.Read())
                {
                    if (objRd.NodeType == XmlNodeType.Element)
                    {
                        if (objRd.HasAttributes)
                        {
                            listBox1.Items.Add("the element " + objRd.Name.ToString()
                                + "has " + objRd.AttributeCount.ToString() + " attributes.");
                            listBox1.Items.Add("they are:");
                            while (objRd.MoveToNextAttribute())
                            {
                                listBox1.Items.Add(objRd.Name + "=" + objRd.Value);
                            }
                        }
                    }
                }

            }
            catch(Exception ex)
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strFilePath = "D:\\test.xml";
            string xpath = "//Transform[@rotation]";
            XmlDocument doc = XmlDocOperate.getXmlDoc(strFilePath);
            XmlNodeList objList = doc.SelectNodes(xpath);//XmlReadAndWrite.getXmlXmlNodeList(strFilePath,xpath);
            foreach (XmlNode objnode in objList)
            {
                XmlNodeReader objRd = new XmlNodeReader(objnode);
                while (objRd.Read())
                {
                    listBox1.Items.Add(objRd.Name);
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string filename = "WriteXml.xml";
            XmlTextWriter objTW = new XmlTextWriter(filename,Encoding.UTF8);

            objTW.WriteStartDocument();
            objTW.WriteStartElement("el","Contact","http://");
            objTW.WriteEndElement();
            objTW.WriteEndDocument();
            objTW.Flush();
            objTW.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //Box b = new Box();
            //b.DEF = "test";
            //Sphere s = new Sphere();
            //s.containerField = "sphere";
            //Material m = new Material();
            //m.shininess = "1.0";
            //Appearance a = new Appearance();
            //a.material = m;
            //a.texture = "sdfsaf";
            //IndexedLineSet line = new IndexedLineSet();
            //IndexedFaceSet face = new IndexedFaceSet();
            //Shape sh = new Shape();
            //sh.appearance = a;
            //sh.gemeotry = face;
            Transform tr = new Transform("0.3 0.3 0.3");
            tr.containerField = "gemeotry";
            tr.addChildren(new Transform());
            tr.addChildren(new LOD());
            tr.addChildren(new IndexedFaceSet());
            field f = new field("test", "SFInt32", "inputOutput");
            Script scr = new Script();
            scr.addChildren(f);
            tr.addChildren(scr);
            scr.addChildren("<!CADA[[]]>");
            ProtoDeclare pd = new ProtoDeclare();
            ProtoBody pb = new ProtoBody();
            ProtoInterface pi = new ProtoInterface();
            pd.protoBody = pb;
            pd.protoInterface = pi;
            pb.addChildren(tr);
            pb.addChildren(scr);
            pi.addChildren(f);
            IS Is = new IS();
            Is.addChildren(new connect("sadfa", "sadfaf"));
            pb.addChildren(Is);
            MessageBox.Show(X3DString.toString(pd));
        }

        private void creatX3D_Click(object sender, EventArgs e)
        {
            string x3dpath = "x3dtest.x3d";
            Box mybox = new Box();
            Material mymaterial = new Material("0.5 0.5 0.5");
            Appearance myapp = new Appearance(mymaterial);
            Shape myshape = new Shape(myapp,mybox);
            //MessageBox.Show(X3DObject.toString(myshape));
            //string myx3d = X3DString.getX3dHead()+"\n"
            //    + X3DObject.toString(myshape)
            //    +"\n"+X3DString.getX3dEnd();
            string myx3d = X3DString.getX3dAll(myshape);
            MessageBox.Show(myx3d);
            //XmlDocument doc = XmlDocOperate.getXmlDoc(x3dpath);            
            XmlDocument doc = XmlDocOperate.loadXmlByStr(myx3d);
            MessageBox.Show("成功保存x3d文件到"+x3dpath);
            doc.Save(x3dpath);
        }

        private void tree_Click(object sender, EventArgs e)
        {
            //Tree mytree = new Tree();
            string myx3d = X3DString.getX3dAll(Tree.getTreeSence(Tree.getTreeGroup(),2));
            //string myx3d = X3DString.getX3dAll(Tree.getTreeGroup());
            MessageBox.Show(myx3d);
            XmlDocument doc = XmlDocOperate.loadXmlByStr(myx3d);
            MessageBox.Show("成功保存x3d文件");
            doc.Save("tree.x3d");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void createFire_Click(object sender, EventArgs e)
        {
            Fire fire = new Fire();
            fire.CreateProto();
        }
    }
}
